class Client < ActiveRecord::Base
  has_many :messages
  has_many :subscribers
  has_many :subscriptions , :through => :subscribers
#   has_many                :messages , :dependent => :destroy
#   validates_presence_of   :nicks , :room , :type
#   validates_uniqueness_of :room
  before_save             :escape_room_name , :generate_secret
  after_save              :prefill_messages


  def escape_room_name ; self.room = URI.escape self.room ; end ;

  def generate_secret
    secrets     = Client.all.map { | a_client | a_client.secret }
    self.secret = SecureRandom.hex while self.secret.nil? || (secrets.include? self.secret)
  end

  def prefill_messages
    N_MESSAGES_QUEUED.times { self.messages.create PRIME_MESSAGE_PARAMS }
  end
end

class IrcClient < Client ; end ;

class SimClient < Client ; end ;
